package com.rahms.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rahms.pojo.entity.CommunityAnnouncement;
import com.rahms.server.mapper.CommunityAnnouncementMapper;
import com.rahms.server.service.CommunityAnnouncementService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社区公告服务实现类
 */
@Service
public class CommunityAnnouncementServiceImpl extends ServiceImpl<CommunityAnnouncementMapper, CommunityAnnouncement> implements CommunityAnnouncementService {
    
    @Override
    public List<CommunityAnnouncement> getLatestAnnouncements(Integer limit) {
        LambdaQueryWrapper<CommunityAnnouncement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommunityAnnouncement::getStatus, 1)  // 只查询已发布的公告
                   .orderByDesc(CommunityAnnouncement::getPublishTime)
                   .last(limit != null && limit > 0, "LIMIT " + limit);
        return this.list(queryWrapper);
    }
    
    @Override
    public List<CommunityAnnouncement> getAnnouncementsByCategory(String category, Integer limit) {
        LambdaQueryWrapper<CommunityAnnouncement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommunityAnnouncement::getStatus, 1)  // 只查询已发布的公告
                   .eq(category != null && !category.isEmpty(), CommunityAnnouncement::getCategory, category)
                   .orderByDesc(CommunityAnnouncement::getPublishTime)
                   .last(limit != null && limit > 0, "LIMIT " + limit);
        return this.list(queryWrapper);
    }
    
    @Override
    public boolean increaseViewCount(Long announcementId) {
        CommunityAnnouncement announcement = this.getById(announcementId);
        if (announcement != null) {
            announcement.setViewCount(announcement.getViewCount() + 1);
            return this.updateById(announcement);
        }
        return false;
    }
}